package com.niuke;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * 47. 数值同化
 * 只找到矩阵扩散，类似 3.宜居星球改造计划 和 36.计算疫情扩散时间
 * https://blog.csdn.net/BYZLZ119/article/details/131146987
 */
public class NiukeMoni47 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        String[] input = scanner.nextLine().split(",");

        // 数组前两个代表 M * N 的矩阵数据
        int m = Integer.parseInt(input[0]);
        int n = Integer.parseInt(input[1]);
        int[] p1 = new int[]{Integer.parseInt(input[2]), Integer.parseInt(input[3])};
        int[] p2 = new int[]{Integer.parseInt(input[4]), Integer.parseInt(input[5])};
        int[][] dp = new int[m][n];
        dp[p1[0]][p1[1]] = 1;
        dp[p2[0]][p2[1]] = 1;

        Queue<int[]> queue = new LinkedList<>();
        queue.offer(p1);
        queue.offer(p2);

        int count = m * n - 2; // 已经有两个作为扩散点了
        int[][] offsets = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

        int day = 1;
        while (!queue.isEmpty() && count > 0) {
            int[] p = queue.poll(); // 弹出并获取到队列首部对象
            int x = p[0];
            int y = p[1];
            day = dp[x][y] + 1;

            for (int[] offset : offsets) {
                int newX = x + offset[0];
                int newY = y + offset[1];

                if (newX >= 0 && newX < m && newY >= 0 && newY < n && dp[newX][newY] == 0) {
                    dp[newX][newY] = day;
                    queue.offer(new int[]{newX, newY});
                    count --;
                }
            }
        }

        System.out.println(day - 1);
    }

}
